﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Web;
using System.Web.Http;
using DAO;
using BUS;

namespace AjaxChatService.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    public class FriendshipsController : ApiController
    {
        FriendshipBUS friendshipBUS = new FriendshipBUS();

        // GET api/Friendships?userId=xxx
        public IEnumerable<User> GetFriendByUserId(int userId)
        {
            var users = friendshipBUS.GetFriendByUserId(userId).ToList();

            return users;
        }

        // POST api/Friendships?userId=xxx&friendName=xxx
        public HttpResponseMessage PostFriendship(int userId, string friendName)
        {
            if(friendshipBUS.AddFriendship(userId, friendName))
                return Request.CreateResponse(HttpStatusCode.OK);

            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

        //DELETE api/Friendships?userId=xxx&friendName=xxx
        public HttpResponseMessage PostDeleteFriendShip(int userId, string friendName, string del)
        {
            if (friendshipBUS.DeleteFriendship(userId, friendName))
                return Request.CreateResponse(HttpStatusCode.OK);

            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

        public HttpResponseMessage PostImportFriend(string userName, string password, int userId)
        {
            if(friendshipBUS.ImportFriend(userName, password, userId))
                return Request.CreateResponse(HttpStatusCode.OK);

            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}